<template>
  <!-- 导航栏 -->
  <van-nav-bar title="确认订单" left-arrow @click-left="back"> </van-nav-bar>
  <!-- 地址信息 -->
  <!-- 添加联系人 -->
  <van-contact-card
    type="add"
    @click="toAddressPage"
    v-if="JSON.stringify(address.value) == '{}'"
  />
  <!-- 编辑联系人 -->
  <van-contact-card
    type="edit"
    :tel="address.tel"
    :name="address.name"
    @click="toAddressPage"
    v-else
  />
  <!-- 商品信息 -->
  <van-card
    v-for="item in list"
    :key="item.id"
    :price="item.total"
    :desc="'库存:' + item.product.stock"
    :title="item.product.name"
    :thumb="item.product.thumb_text"
    @click-thumb="toProductInfo(item.proid)"
    :num="item.nums"
  >
    <template #tags>
      <div style="color: #969799">单价: {{ item.price }}</div>
    </template>
  </van-card>
  <!--订单备注 -->
  <van-field
    v-model="remark"
    rows="2"
    autosize
    label="订单备注"
    type="textarea"
    maxlength="120"
    placeholder="请输入订单备注"
    show-word-limit
  />
  <!--合计金额 -->
  <van-submit-bar :price="total" button-text="确认订单" @submit="onSubmit" />
</template>
<script setup>
import { onBeforeMount } from "vue";

const { proxy } = getCurrentInstance();
const back = () => proxy.$router.go(-1);

let business = proxy.$cookies.get("business") ? proxy.$cookies.get("business") : {};
business = reactive(business);

let address = proxy.$cookies.get("address") ? proxy.$cookies.get("address") : {};
address = ref(address);

// console.log(address);
let cartids = proxy.$route.query.hasOwnProperty("cartids")
  ? proxy.$route.query.cartids
  : 0;
cartids = ref(cartids);

const list = ref([]);
const remark = ref("");
const toAddressPage = () => {
  proxy.$router.push({
    path: "/address/index",
    query: { action: "order" },
  });
};

onBeforeMount(() => {
  if (JSON.stringify(address.value) == "{}") {
    getAddressData();
  }
  getCartData();
});

//获取商品数据
const getCartData = async () => {
  let result = await proxy.$POST({
    url: "/cart/index",
    params: {
      busid: business.id,
      cartids: cartids.value,
    },
  });
  if (result.code == 1) {
    list.value = result.data;
  }
};

const getAddressData = async () => {
  let result = await proxy.$POST({
    url: "/address/selected",
    params: {
      busid: business.id,
    },
  });
  if (result.code == 1) {
    address.value = result.data;
    proxy.$cookies.set("address", result.data);
  }
};
// 计算总价
const total = computed(() => {
  let nums = list.value.reduce(
    (accumulator, currentValue) => accumulator + parseFloat(currentValue.total),
    0
  );
  return nums * 100;
});

//购买商品
const onSubmit = async () => {
  proxy.$confirm({
    message: "是否确认购买该商品",
  })
  .then(async () => {
    let data = {
    busid: business.id,
    businessaddrid: address.value.id,
    amount: (total.value / 100).toFixed(2),
    remark: remark.value,
    cartids: cartids.value,
  };
  // console.log(data)
  let result = await proxy.$POST({
    url: "/order/add",
    params: data,
  });

  if(result && result.code == 0)
  {
    proxy.$fail(result.msg);
    return false;
  }else
  {
    proxy.$success({
      message: result.msg,
      duration: 1000,
      onClose: () => {
        proxy.$router.push('/order/index');
        return false;
      }
    })
  }
  })
  .catch(() => {});
  
};

const toProductInfo = (proid) => {
  proxy.$router.push({
    path: "/product/info",
    query: { proid: proid },
  });
};
</script>
